package com.martin.lib_base.impl

import android.app.Activity
import android.app.Application.ActivityLifecycleCallbacks
import android.os.Bundle
import com.martin.lib_base.helper.AppLifecycleHelper
import com.martin.lib_base.ktx.loge
import dev.utils.app.AppUtils

/**
 * 描述：Activity 生命周期回调
 */
class ActivityLifecycleCallBackImpl : ActivityLifecycleCallbacks {

    // 是否处于前台
    private var isForeground = false

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        loge("启动的Activity : ${activity.javaClass.simpleName}")
    }

    override fun onActivityStarted(activity: Activity) {
        if (AppUtils.isAppForeground() && !isForeground) {
            isForeground = true
            AppLifecycleHelper.onAppShow()
        }
    }

    override fun onActivityResumed(activity: Activity) {

    }

    override fun onActivityPaused(activity: Activity) {
    }

    override fun onActivityStopped(activity: Activity) {
        if (!AppUtils.isAppForeground() && isForeground) {
            isForeground = false
            AppLifecycleHelper.onAppHide()
        }
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
    }

    override fun onActivityDestroyed(activity: Activity) {
        loge("关闭的Activity : ${activity.javaClass.simpleName}")
    }
}